/*
This code attempts to estimate treatment effects of ppp
*/

do "${dodir}/make_globals.do"

////////////////////////////////////////////////////////////////////////////////
// step 1: emp growth regs
////////////////////////////////////////////////////////////////////////////////

use dateapproved_first id q min_emp_2018 min_emp_2019 min_emp_2020q1 naics_2_mode st_mode state ///
med_wage total_compensation num_employees g using ${datadir}/reg_data, clear

// drop if 0 or 1 employees at any point in 2018 or 2019
drop if min_emp_2018==0 | min_emp_2018==1 | min_emp_2019==0 | min_emp_2019==1 | min_emp_2020q1==0 | min_emp_2020q1==1

// drop if two digit naics is missing
drop if naics_2_mode==. | naics_2_mode==0

// drop if state is missing
drop if st_mode==0 | st_mode==.

// drop if never treated
keep if g>0 & g~=.	

// drop restuarants (special covid treatment), public admin, and utilities
keep if (naics_2_mode~=92 & naics_2_mode~=22 & naics_2_mode~=72 & naics_2_mode~=49 & naics_2_mode~=55)

// clean the compensation variables
replace med_wage=0 if med_wage==.
replace total_compensation=0 if total_compensation==.

// have wages be the max of med_wages and total_compensation
replace med_wage=max(med_wage, total_compensation)

gen mean_comp=med_wage/num_employees

// replace med_wage to be "100000*employee count" if mean comp exceeds 100k
replace med_wage=100000*num_employees if mean_comp>100000 & mean_comp~=.

// size bin
g firm_size_bin=0 if min_emp_2018<10
	replace firm_size_bin=1 if min_emp_2018>=10 & min_emp_2018<50
	replace firm_size_bin=2 if min_emp_2018>=50 & min_emp_2018<100
	replace firm_size_bin=3 if min_emp_2018>=100 & min_emp_2018<250
	replace firm_size_bin=4 if min_emp_2018>=250
	
// outcome variable: employment relative to the average quarterly employment in 2018 and 2019
	// percentage change in employment

// time series
tsset id q
	
g emp_2018=l8.num_employees if q>=9 & q<=12
	replace emp_2018=l4.num_employees if q>=5 & q<=8
	replace emp_2018=l12.num_employees if q>=13 & q<=16
	replace emp_2018=l16.num_employees if q>16

g emp_2019=l4.num_employees if q>=9 & q<=12
	replace emp_2019=l8.num_employees if q>=13 & q<=16
	replace emp_2019=l12.num_employees if q>16

// outcome variable: employment relative to the average quarterly employment in 2018 and 2019
g y=num_employees/emp_2018 if q>4

// cap outcome variable at 10
	replace y=10 if y>10 & y!=.

// drop if y is missing after the base year
gen deerop1=0
replace deerop1=1 if y==. & q>4
egen deerop2=max(deerop1), by(id)
drop if deerop2==1
drop deerop1 deerop2	

// create cell identifiers
preserve

	gcollapse (mean) y_m=y, by(naics_2_mode st_mode firm_size_bin)
	
	g id=_n
	
	save $datadir/y_m, replace

restore

merge m:1 naics_2_mode st_mode firm_size_bin using $datadir/y_m, update replace nogen

g y_dm=y-y_m

csdid y_dm if (st_mode==39 | st_mode==42), i(id) t(q) g(g) method(dripw) wboot notyet

estat event

csdid_plot , graphregion(color(white)) bgcolor(white) ///
ylabel(,format(%9.2fc)) ///
ytitle("Growth in Employees (Base Year: 2018)") ///
xtitle("Quarters to Treatment") xscale(r(-8 4)) xlabel(-8(2)4,format(%9.0fc)) ///
yscale(r(-0.05 0.20)) ylabel(-0.05(0.05)0.2,format(%9.2fc)) 
graph export $outdir/emp_growth_bin_onlynynj.png, replace width(3000)

csdid y_dm if st_mode!=39 & st_mode!=42, i(id) t(q) g(g) method(dripw) wboot notyet

estat event

csdid_plot , graphregion(color(white)) bgcolor(white) ///
ylabel(,format(%9.2fc)) ///
ytitle("Growth in Employees (Base Year: 2018)") ///
xtitle("Quarters to Treatment") xscale(r(-8 4)) xlabel(-8(2)4,format(%9.0fc))  ///
yscale(r(-0.05 0.20)) ylabel(-0.05(0.05)0.2,format(%9.2fc)) 
graph export $outdir/emp_growth_bin_notnynj.png, replace width(3000)
